package com.wgo.precise.client.ui.view.projecttree;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;

import com.wgo.precise.client.ui.controller.Registry;
import com.wgo.precise.client.ui.controller.RequirementPlugin;
import com.wgo.precise.client.ui.model.NodeChild;
import com.wgo.precise.client.ui.model.facade.INodeParent;


/**
 * 
 * @author Petter L. H. Eide
 * @version $Id: CreateAction.java,v 1.4 2006-02-06 09:29:08 petterei Exp $
 */
public abstract class CreateAction<T extends NodeChild, ParentT extends INodeParent> extends CrudAction<ParentT> {

    protected T newItem;

    protected CreateAction(String text, ParentT item, ImageDescriptor image) {
        super(text, item, image);
        initCreateAction();
    }

    protected static String createText(String objectType){
        return "Create " + objectType ;
    }
    
    private void initCreateAction() {
        if (getItem() != null) {
            String message = "Create new " + getText();
            setToolTipText(getText()+ ": " +  message);

            setMessage(message);

        }
    }

    /**
     * @see com.wgo.precise.client.ui.view.projecttree.CrudAction#run()
     */
    @Override
    public void run() {
        if (newItem != null && getViewer() != null) {
            try {
                ((AbstractTreeViewer) getViewer()).add(getItem(), newItem);
            } catch (ClassCastException e) {
                RequirementPlugin.log(e);
            }
            ((TreeViewer)getViewer()).setExpandedState(getItem(), true);
            ((TreeViewer)getViewer()).reveal(newItem);
            getViewer().setSelection(new StructuredSelection(newItem), true);
            RequirementPlugin.getInstance().getPreciseSubject().notifyNewModelItemCreated(newItem);
        }
        RequirementPlugin.getInstance().getEditorWindowManager().startEditorPart(Registry.MODEL_EDITOR, newItem);
        super.run();
    }

    protected void setNewItem(T newItem) {
        this.newItem = newItem;
    }

}
/**
 * 
 * Changelog:
 * 
 * $Log: CreateAction.java,v $
 * Revision 1.4  2006-02-06 09:29:08  petterei
 * - extracted window-managing from requirements plugin to seperate class
 * - moved child-parent-logic to super-classes
 *
 * Revision 1.3  2006-01-31 22:40:30  petterei
 * tuned the editor window management
 *
 * Revision 1.2  2006-01-30 21:52:33  petterei
 * implemented wrapper factory and lazy wrapper-instantiation of parents
 * implemented a "system" for each top-node type
 * added top-categories for all top-nodes
 * implemented some interfaces for the wrappers, and started using these
 *
 * Revision 1.1  2006-01-23 19:58:38  petterei
 * initial commit for client, fitting the alternative session implementation
 *
 * Revision 1.8  2006-01-13 08:13:00  petterei
 * Using Long consequently to differentiate between �null� and �0� for dbId and conceptId (no boxing/unboxing)
 * Ajusted code to stronger java-syntax
 *
 * Revision 1.7  2005-12-14 20:27:21  petterei
 * added custom images
 *
 * Revision 1.6  2005-12-14 08:22:04  petterei
 * -refactored: new categories inherit propertysets from parent category, requirements-propertysets is only retrieved from parent category (before: no "physical" category inheritance, but requirement got propertysets from all categoryparents)
 * -cleaned up some code and configfiles, minor refactoring
 *
 * Revision 1.5  2005-12-13 12:10:54  petterei
 * refactorin + bugfix
 *
 * Revision 1.4  2005-12-12 05:58:15  petterei
 * - fixed columns-with in properties-editor
 * - udate of property-sets-tables
 *
 * Revision 1.3  2005-12-05 04:16:41  petterei
 * *** empty log message ***
 *
 * Revision 1.2  2005/09/30 15:33:23  petterei
 * *** empty log message ***
 *
 * Revision 1.1  2005/09/27 21:06:18  petterei
 * *** empty log message ***
 *
 * Revision 1.3  2005/09/27 17:06:19  petterei
 * *** empty log message ***
 *
 * Revision 1.2  2005/08/23 06:40:47  peide3
 * *** empty log message ***
 *
 * Revision 1.2  2005/08/09 09:24:06  petterei
 * *** empty log message ***
 *
 * Revision 1.1  2005/07/31 22:55:09  petterei
 * initial commit
 *
 * Revision 1.1  2005/07/20 08:48:40  peide3
 * updated and implemented more functionality
 *
 */
